import reader
import numpy as np
import pandas as pd
import glob
import cv2
import os

def label_marker():
    """
    以独热方式标记新的数据
    :return:
    """
    out_file_path_s = './csv/label_s.csv'
    out_file_path_m = './csv/label_m.csv'
    cld_dir = 'G:/CLP/CLP_2019_0020'
    file_lst = glob.glob(cld_dir+'/*.*')

    for file in file_lst:
        date_index = file.rfind('2019')
        date = file[date_index:date_index+8]
        label_s_lst = []
        label_m_lst = []

        cltype = reader.read_cld(file)
        cltype = cv2.resize(cltype,(6001,6001),interpolation=cv2.INTER_NEAREST)

        for i in range(11):
            for j in range(26):
                # rs_patch = rs_img[i*200:i*200+1000,j*200:j*200+1000]
                cld_patch = cltype[i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                label_s = reader.label_s(cld_patch).astype(np.int32).astype(str)
                label_m = reader.label_m(cld_patch).astype(np.int32).astype(str)
                patch_name = date + '_%02d_%02d.png' % (i+1, j+1)
                label_s = np.hstack(([patch_name],label_s))
                label_m = np.hstack(([patch_name],label_m))
                print(label_s, label_m)
                label_s_lst.append(label_s)
                label_m_lst.append(label_m)


        new_df_s = pd.DataFrame.from_dict(label_s_lst, orient='columns')
        new_df_m = pd.DataFrame.from_dict(label_m_lst, orient='columns')

        if date=='20190101':
            new_df_s.to_csv(out_file_path_s, mode='w', header=False)
            new_df_m.to_csv(out_file_path_m, mode='w', header=False)
        else:
            new_df_s.to_csv(out_file_path_s, mode='a', header=False)
            new_df_m.to_csv(out_file_path_m, mode='a', header=False)

def label_modify_s(cld_dir,csv_path):
    """
    使用规则对单标签数据的csv文件进行更新
    :param cld_dir: CLP产品目录
    :param csv_path: 原csv文件路径
    :return:
    """
    thr = 0.25
    modified = 0
    cld_names = ['Ci','Cs','DC','Ac','As','Ns','Cu','Sc','St']

    file_lst = glob.glob(cld_dir + '/*.*')
    file = pd.read_csv(csv_path, header=None)
    lines = file.values

    for file in file_lst:
        date_index = file.rfind('2019')
        date = file[date_index:date_index + 8]
        label_s_lst = []
        label_m_lst = []

        cltype = reader.read_cld(file)
        cltype = cv2.resize(cltype, (6001, 6001), interpolation=cv2.INTER_NEAREST)

        for i in range(11):
            for j in range(26):
                # rs_patch = rs_img[i*200:i*200+1000,j*200:j*200+1000]
                cld_patch = cltype[i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                label_s = reader.label_s(cld_patch).astype(np.int32).astype(str)
                #label_m = reader.label_m(cld_patch).astype(np.int32).astype(str)
                patch_name = date + '_%02d_%02d.png' % (i + 1, j + 1)

                cld_ratio = reader.cal_ratio(cld_patch)

                patch_index = np.where(lines==patch_name)[0] #找到对应的行号
                former_class = lines[patch_index,1]
                if (lines[patch_index,1]=='PatchElse'\
                    or lines[patch_index,1]=='LowWaterCloud'\
                    or lines[patch_index,1]=='HighIceCloud'\
                    or reader.more_cloud(cld_patch))\
                        and lines[patch_index,1]!='TropicalCyclone' \
                        and lines[patch_index, 1] != 'ExtratropicalCyclone'\
                        and lines[patch_index,1]!='WesterlyJet'\
                        and lines[patch_index,1]!='FrontalSurface'\
                        and lines[patch_index,1]!='Snow':

                    if np.max(cld_ratio[1:10])>thr:
                        max_cloud = np.argmax(cld_ratio[1:10])
                        lines[patch_index,1] = cld_names[max_cloud]
                    else :
                        lines[patch_index, 1] = 'PatchElse'

                    if(former_class!=lines[patch_index, 1]):
                        modified += 1
                        print(patch_name, former_class, '-->',lines[patch_index, 1])



                #label_s = np.hstack(([patch_name], label_s))
                #label_m = np.hstack(([patch_name], label_m))

                label_s_lst.append(label_s)
    print("%d modified" % modified)
    out_path = csv_path.replace('.CSV', '_modified.CSV')
    out_path = out_path.replace('.csv', '_modified.csv')
    data = pd.DataFrame.from_dict(lines, orient='columns')

    data.to_csv(out_path,mode='w',index=False)

def cloud_count(cld_dir,csv_path):
    """
    demo
    用于查看各种云的大致数量
    :param cld_dir:
    :param csv_path:
    :return:
    """
    thr = 0.18
    modified = 0
    cnt = [0,0,0,0,0,0,0,0,0]
    st_ratio = []
    st_thr = 0
    cld_names = ['Ci', 'Cs', 'DC', 'Ac', 'As' ,'Ns', 'Cu', 'Sc', 'St']

    file_lst = glob.glob(cld_dir + '/*.*')

    csv_file = pd.read_csv(csv_path, header=None)
    lines = csv_file.values

    for file in file_lst:
        date_index = file.rfind('2019')
        date = file[date_index:date_index + 8]

        cltype = reader.read_cld(file)
        cltype = cv2.resize(cltype, (6001, 6001), interpolation=cv2.INTER_NEAREST)

        for i in range(11):
            for j in range(26):


                # rs_patch = rs_img[i*200:i*200+1000,j*200:j*200+1000]
                cld_patch = cltype[i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                label_s = reader.label_s(cld_patch).astype(np.int32).astype(str)
                # label_m = reader.label_m(cld_patch).astype(np.int32).astype(str)
                patch_name = date + '_%02d_%02d.png' % (i + 1, j + 1)

                patch_index = np.where(lines == patch_name)[0]  # 找到对应的行号

                former_class = lines[patch_index, 1]
                if (former_class == 'PatchElse' \
                    or former_class == 'LowWaterCloud' \
                    or former_class == 'HighIceCloud' \
                    or reader.more_cloud(cld_patch)) \
                        and former_class != 'TropicalCyclone' \
                        and former_class != 'ExtratropicalCyclone' \
                        and former_class != 'WesterlyJet' \
                        and former_class != 'FrontalSurface' \
                        and former_class != 'Snow':


                    cld_mark = reader.label_s(cld_patch, thr)
                    cld_ratio = reader.cal_ratio(cld_patch)
                    st_ratio.append(cld_ratio[8])
                    if cld_ratio[8] >= thr:
                        st_thr += 1
                    print(patch_name,np.max(st_ratio), '%.4f' % np.median(st_ratio), st_thr)
                    cnt += cld_mark


                #print(patch_name,cnt,former_class,'\t\t',cld_ratio[4],cld_ratio[8])

def crop_specific_cloud(rs_dir,cld_dir,out_dir,type,type_name,thr=0.25):
    """

    :param rs_dir:
    :param cld_dir:
    :param out_dir:
    :param csv_path:
    :param type: list
    :param type_name: list
    :param thr:
    :return:
    """
    rs_file_list = glob.glob(rs_dir + '/*.*')
    cld_file_list = glob.glob(cld_dir + '/*.*')

    for yearday in range(len(rs_file_list)):
        rs_date_index = rs_file_list[yearday].rfind('2019')
        cld_date_index = rs_file_list[yearday].rfind('2019')
        rs_date = rs_file_list[yearday][rs_date_index:rs_date_index + 8]
        cld_date = rs_file_list[yearday][cld_date_index:cld_date_index + 8]
        assert rs_date==cld_date

        rs_info = reader.read_rs(rs_file_list[yearday])
        cld_info = reader.read_cld(cld_file_list[yearday])
        cltype = cv2.resize(cld_info,(6001,6001),interpolation=cv2.INTER_NEAREST)

        for i in range(12,26):
            for j in range(26):
                cld_patch = cltype[i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                cld_label = reader.label_s(cld_patch,thr)

                for t in type:
                    if cld_label[t] == 1:
                        rs_b3 = rs_info['albedo_03'][i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                        rs_b4 = rs_info['albedo_04'][i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]
                        rs_b5 = rs_info['albedo_05'][i * 200:i * 200 + 1000, j * 200:j * 200 + 1000]

                        out_path = out_dir + '/' + type_name[t]
                        if not os.path.exists(out_path):
                            os.makedirs(out_path)

                        rs_img = reader.ndarrdy_to_img([rs_b3,rs_b4,rs_b5],out_path,rs_date+'_%02d_%02d.png'%(i,j))


                print(rs_date)
                print(rs_date+'_%02d_%02d.png'%(i,j))





if __name__ == '__main__':
    label_modify_s('G:/CLP/CLP_2019_0020',csv_path='G:\data_copy\LWSCID-S\LWSCID-S_new.CSV')
    #cloud_count('G:/CLP/CLP_2019_0020',csv_path='G:\data_copy\LWSCID-S\LWSCID-S_new.CSV')

    '''
    crop_specific_cloud('G:/2019_0020_nc','G:\CLP\CLP_2019_0020',
                        'G:/data_copy/add',[5-1,9-1],
                        ['Ci', 'Cs', 'DC', 'Ac', 'As' ,'Ns', 'Cu', 'Sc', 'St'],
                        thr=0.25)
    '''